//
//  KFDAnnouncementListViewController.m
//  
//
//  Created by JIRENTIANXIANG on 6/23/15.
//
//

#import "KFDAnnouncementListViewController.h"
#import "KFDAnnouncementListViewModel.h"
#import "KFDAnnouncementTableViewCell.h"
#import "KFDAnnouncementViewController.h"
#import "ui_config.h"

#define KFDAnnouncementTableViewCellReuseIdentifier @"KFDAnnouncementTableViewCell"
#define KFDAnnouncementTableViewCellNibName @"KFDAnnouncementTableViewCell"
#define KFDAnnouncementViewControllerNibName @"KFDAnnouncementViewController"

@interface KFDAnnouncementListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong,nonatomic) KFDAnnouncementListViewModel* viewModel;

@property (strong,nonatomic) UITableView* tableView;

@end

@implementation KFDAnnouncementListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"公告";
    self.tableView=[[UITableView alloc] initWithFrame:self.view.frame];
    self.tableView.backgroundColor=KFD_WHITE;
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    [self.view addSubview:self.tableView];
    self.viewModel=[[KFDAnnouncementListViewModel alloc] init];
    [self.viewModel loadAnnouncementListWithCompletionHandler:^(BOOL success) {
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.announcementList.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KFDAnnouncementTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:KFDAnnouncementTableViewCellReuseIdentifier];
    if (!cell) {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:KFDAnnouncementTableViewCellNibName owner:nil options:nil];
        cell = objects[0];
    }
    [cell setAnnoucement:self.viewModel.announcementList[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 36.5;
}

#pragma mark - Table view delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KFDAnnouncementViewController* vc=[[KFDAnnouncementViewController alloc] initWithNibName:KFDAnnouncementViewControllerNibName bundle:nil];
    KFDAnnouncementBrief* announcement=self.viewModel.announcementList[indexPath.row];
    vc.announcementId=announcement.id;
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
